package uk.ac.cam.juliet.zedcat.socialnetworking;
import java.sql.SQLException;
import java.util.Calendar;

import uk.ac.cam.juliet.zedcat.db.Sighting;
import uk.ac.cam.juliet.zedcat.db.dbAnalysisDatabase;

/**
 *  Stores statistics and downloads/uploads them to the database.
 *  @author Daniel
 */

public class Stats {

	dbAnalysisDatabase db = null;
	Logger lg = null;
	
	int[] play = new int[24];
	int[] food = new int[24];
	int[] sleep = new int[24];
	int[] totals = new int[3];
	
	public Stats(dbAnalysisDatabase db, Logger lg) throws SQLException {
		this.db = db;
		this.lg = lg;
	}

	public void downloadStats() throws NumberFormatException, SQLException {
		for(int i = 0; i<24;i++) {
			String keynum = i<10 ? "0" + i : "" + i;
			sleep[i] = Integer.parseInt(db.getSettingsEntryForKey("SLEEP" + keynum));
		}
		
		for(int i = 0; i<24;i++) {
			String keynum = i<10 ? "0" + i : "" + i;
			food[i] = Integer.parseInt(db.getSettingsEntryForKey("FOOD" + keynum));
		}
		
		for(int i = 0; i<24;i++) {
			String keynum = i<10 ? "0" + i : "" + i;
			play[i] = Integer.parseInt(db.getSettingsEntryForKey("PLAY" + keynum));
		}
		
		totals[0] = Integer.parseInt(db.getSettingsEntryForKey("SLEEPSUM"));
		totals[1] = Integer.parseInt(db.getSettingsEntryForKey("FOODSUM"));
		totals[2] = Integer.parseInt(db.getSettingsEntryForKey("PLAYSUM"));
	}
	
	public void updateStats(Sighting s) throws SQLException {
		
		Calendar c = Calendar.getInstance();
		c.setTime(s.getTime());
		int hour = c.get(Calendar.HOUR_OF_DAY);
		String keynum = hour<10 ? "0" + hour : "" + hour;
		
		if(s.getActionID().equals("1")) {
			//SLEEP
			db.setSettingsEntryForKey("SLEEP"+keynum, Integer.toString(++(sleep[hour])));
			lg.log("SLEEP"+keynum+ " updated to " + sleep[hour]);
			db.setSettingsEntryForKey("SLEEPSUM" , Integer.toString(++(totals[0])));
			lg.log("SLEEPSUM updated to " + totals[0]);
		}
		
		if(s.getActionID().equals("2")) {
			//FOOD
			db.setSettingsEntryForKey("FOOD"+keynum, Integer.toString(++(food[hour])));
			lg.log("FOOD"+keynum+ " updated to " + sleep[hour]);
			db.setSettingsEntryForKey("FOODSUM" , Integer.toString(++(totals[1])));
			lg.log("FOODSUM updated to " + totals[1]);
		}
		
		if(s.getActionID().equals("3")) {
			//PLAY
			db.setSettingsEntryForKey("PLAY"+keynum, Integer.toString(++(play[hour])));
			lg.log("PLAY"+keynum+ " updated to " + sleep[hour]);
			db.setSettingsEntryForKey("PLAYSUM" , Integer.toString(++(totals[2])));
			lg.log("PLAYSUM updated to " + totals[2]);
		}
	}
	
	public void resetStats() throws NumberFormatException, SQLException {
		for(int i = 0; i<24;i++) {
			sleep[i] = 0;
			String keynum = i<10 ? "0" + i : "" + i;
			db.setSettingsEntryForKey("SLEEP" + keynum, "0");
		}
		
		for(int i = 0; i<24;i++) {
			food[i] = 0;
			String keynum = i<10 ? "0" + i : "" + i;
			db.setSettingsEntryForKey("FOOD" + keynum, "0");
		}
		
		for(int i = 0; i<24;i++) {
			play[i] = 0;
			String keynum = i<10 ? "0" + i : "" + i;
			db.setSettingsEntryForKey("PLAY" + keynum, "0");
		}
		
		totals[0] = 0;
		db.setSettingsEntryForKey("SLEEPSUM","0");
		totals[1] = 0;
		db.setSettingsEntryForKey("FOODSUM","0");
		totals[2] = 0;
		db.setSettingsEntryForKey("PLAYSUM","0");
	}
}
